package com.bw.title.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bw.po.TbHouseMessage;
import com.bw.po.TbResource;
import com.bw.result.ResultVo;
import com.bw.title.service.TbHouseMessageService;
import com.bw.title.service.TbResourceService;
import com.bw.title.util.FastFileUtils;
import com.bw.util.SnowflakeIdWorker;
import com.bw.vo.IdVo;
import com.bw.vo.TbMessageVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.UUID;

/**
 * packageName com.bw.title.controller
 * @author LXC_?
 * @version JDK 8
 * @className TbResourceController (此处以class为例)
 * @date 2023/11/14
 */
@Slf4j
@RestController
@RequestMapping("/resource")
public class TbResourceController {
    @Autowired
    private FastFileUtils fastFileUtils;

    @Autowired
    private TbResourceService tbResourceService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    private SnowflakeIdWorker idWorker = new SnowflakeIdWorker(0,0);

    /**
     * 实现多图片上传
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("/uploadAdd")
    public ResultVo uploadAdd(@RequestParam("file")MultipartFile file) throws IOException {
        //FastDFS图片上传
        String uploadFile = fastFileUtils.uploadFile(file);
        //创建实体类对象
        TbResource tbResource = new TbResource();
        //创建数组对象
        List<String> list = new ArrayList<>();
        //将图片信息添加到数组
        list.add(uploadFile);
        //遍历数组
        for (String url : list) {
            //雪花算范计算出id
            tbResource.setId(String.valueOf(idWorker.nextId()));
            //生成图片的随机UUID
            tbResource.setResourceId(String.valueOf(UUID.randomUUID()));
            //生成图片信息
            tbResource.setUrl(url);
        }
        //添加图片信息
        tbResourceService.save(tbResource);
        //redis接收图片UUID
        redisTemplate.opsForValue().set("tbResource -> resourceId：",tbResource.getResourceId());
        return ResultVo.SUCCESS();
    }




}
